/*
 * UserForm2.java
 *
 * Created on February 8, 2008, 6:08 PM
 */
package com.floreantpos.ui.forms;

import java.util.List;

import javax.swing.DefaultComboBoxModel;

import com.floreantpos.PosException;
import com.floreantpos.model.User;
import com.floreantpos.model.UserType;
import com.floreantpos.model.dao.UserDAO;
import com.floreantpos.model.dao.UserTypeDAO;
import com.floreantpos.model.util.IllegalModelStateException;
import com.floreantpos.ui.BeanEditor;
import com.floreantpos.ui.dialog.POSMessageDialog;
import com.floreantpos.util.POSUtil;

/**
 *
 * @author rodaya
 */
public class UserForm extends BeanEditor {

  /**
   * Creates new form UserForm2
   */
  public UserForm() {
    initComponents();

    UserTypeDAO dao = new UserTypeDAO();
    List<UserType> userTypes = dao.findAll();

    cbUserType.setModel(new DefaultComboBoxModel(userTypes.toArray()));
  }

  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();
    jLabel10 = new javax.swing.JLabel();
    tfPassword1 = new javax.swing.JPasswordField();
    tfPassword2 = new javax.swing.JPasswordField();
    tfId = new javax.swing.JTextField();
    tfSsn = new javax.swing.JTextField();
    tfFirstName = new javax.swing.JTextField();
    tfLastName = new javax.swing.JTextField();
    jLabel6 = new javax.swing.JLabel();
    cbUserType = new javax.swing.JComboBox();

    setLayout(new java.awt.GridBagLayout());

    jLabel1.setText(com.floreantpos.POSConstants.IDENTIFICATION + ":");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
    add(jLabel1, gridBagConstraints);

    jLabel2.setText(com.floreantpos.POSConstants.DOCUMENT);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
    add(jLabel2, gridBagConstraints);

    jLabel3.setText(com.floreantpos.POSConstants.FIRST_NAME + ":");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
    add(jLabel3, gridBagConstraints);

    jLabel4.setText(com.floreantpos.POSConstants.LAST_NAME + ":");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
    add(jLabel4, gridBagConstraints);

    jLabel9.setText(com.floreantpos.POSConstants.ENTER_YOUR_PASSWORD + ":");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
    add(jLabel9, gridBagConstraints);

    jLabel10.setText(com.floreantpos.POSConstants.CONFIRM_PASSWORD);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 5;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
    add(jLabel10, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.gridwidth = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.ipadx = 1;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
    add(tfPassword1, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 5;
    gridBagConstraints.gridwidth = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.ipadx = 1;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
    add(tfPassword2, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.ipadx = 100;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
    add(tfId, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.ipadx = 100;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
    add(tfSsn, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.gridwidth = 6;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.ipadx = 200;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
    add(tfFirstName, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.gridwidth = 6;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.ipadx = 200;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
    add(tfLastName, gridBagConstraints);

    jLabel6.setText(com.floreantpos.POSConstants.TYPE + ":");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 8;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    add(jLabel6, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 8;
    gridBagConstraints.gridwidth = 8;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
    add(cbUserType, gridBagConstraints);
  }

  private javax.swing.JComboBox cbUserType;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel10;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JLabel jLabel9;
  private javax.swing.JTextField tfFirstName;
  private javax.swing.JTextField tfId;
  private javax.swing.JTextField tfLastName;
  private javax.swing.JPasswordField tfPassword1;
  private javax.swing.JPasswordField tfPassword2;
  private javax.swing.JTextField tfSsn;

  @Override
  public void dispose() {

  }

  @Override
  public String getDisplayText() {
    if (isEditMode()) {
      return com.floreantpos.POSConstants.CHANGE_SERVER;
    }

    return com.floreantpos.POSConstants.INCLUDE_SERVER;
  }

  private boolean editMode;

  @Override
  public boolean save() {
    try {
      updateModel();
    } catch (IllegalModelStateException e) {
      POSMessageDialog.showError(this, e.getMessage());
      return false;
    }

    User user = (User) getBean();
    UserDAO userDAO = UserDAO.getInstance();

    if (!editMode) {
      if (userDAO.isUserExist(user.getUserId())) {
        POSMessageDialog.showError(this, com.floreantpos.POSConstants.DUPLICATED_SERVER + user.getUserId() + ".");
        return false;
      }
    }

    try {
      userDAO.saveOrUpdate(user, editMode);
    } catch (PosException x) {
      POSMessageDialog.showError(this, x.getMessage(), x);
      x.printStackTrace();
      return false;
    } catch (Exception x) {
      POSMessageDialog.showError(this, com.floreantpos.POSConstants.IMPOSSIBLE_WRITING_SERVER, x);
      x.printStackTrace();
      return false;
    }

    return true;
  }

  @Override
  protected boolean updateModel() throws IllegalModelStateException {
    User user = null;
    if (!(getBean() instanceof User)) {
      user = new User();
    } else {
      user = (User) getBean();
    }

    int id = 1000;
    try {
      id = Integer.parseInt(tfId.getText());
    } catch (Exception x) {
      throw new IllegalModelStateException(com.floreantpos.POSConstants.INVALID_ID);
    }

    String ssn = tfSsn.getText();
    String firstName = tfFirstName.getText();
    String lastName = tfLastName.getText();
    String password1 = new String(tfPassword1.getPassword());
    String password2 = new String(tfPassword2.getPassword());

    if (POSUtil.isBlankOrNull(ssn)) {
      throw new IllegalModelStateException(com.floreantpos.POSConstants.DOCUMENT_REQUIRED);
    }
    if (POSUtil.isBlankOrNull(firstName)) {
      throw new IllegalModelStateException(com.floreantpos.POSConstants.NAME_REQUIRED);
    }
    if (POSUtil.isBlankOrNull(lastName)) {
      throw new IllegalModelStateException(com.floreantpos.POSConstants.LAST_NAME_REQUIRED);
    }
    if (POSUtil.isBlankOrNull(password1)) {
      throw new IllegalModelStateException(com.floreantpos.POSConstants.PASSWORD_REQUIRED);
    }
    if (!POSUtil.isNumeric(password1)) {
      throw new IllegalModelStateException(com.floreantpos.POSConstants.PASSWORD_ONLY_NUMBERS);
    }
    if (POSUtil.isBlankOrNull(password2)) {
      throw new IllegalModelStateException(com.floreantpos.POSConstants.PASSWORD_CONFIRMATION_REQUIRED);
    }
    if (!password1.equals(password2)) {
      throw new IllegalModelStateException(com.floreantpos.POSConstants.PASSWORD_ARE_DIFFERENTS);
    }

    double cost = 0;

    user.setNewUserType((UserType) cbUserType.getSelectedItem());
    user.setCostPerHour(cost);

    user.setSsn(ssn);
    user.setUserId(id);
    user.setFirstName(firstName);
    user.setLastName(lastName);
    user.setPassword(password1);

    setBean(user);
    return true;
  }

  @Override
  protected void updateView() {
    if (!(getBean() instanceof User)) {
      return;
    }
    User user = (User) getBean();
    setData(user);
  }

  private void setData(User data) {
    if (data.getUserId() != null) {
      tfId.setText(String.valueOf(data.getUserId()));
    } else {
      tfId.setText("");
    }
    if (data.getSsn() != null) {
      tfSsn.setText(data.getSsn());
    } else {
      tfSsn.setText("");
    }
    tfFirstName.setText(data.getFirstName());
    tfLastName.setText(data.getLastName());
    tfPassword1.setText(data.getPassword());
    tfPassword2.setText(data.getPassword());

    cbUserType.setSelectedItem(data.getNewUserType());

  }

  public boolean isEditMode() {
    return editMode;
  }

  public void setEditMode(boolean editMode) {
    this.editMode = editMode;
    if (editMode) {
      tfId.setEditable(false);
    } else {
      tfId.setEditable(true);
    }
  }

  public void setId(Integer id) {
    if (id != null) {
      tfId.setText(String.valueOf(id.intValue()));
    }
  }
}
